package org.tools.ui.view
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.text.TextLineMetrics;
	
	import mx.controls.Label;
	import mx.core.IFlexDisplayObject;
	import mx.core.UIComponent;
	
	/**
	 * 仿超链接
	 * @author zjm  Created: 2012-1-14 19:59:31
	 */
	[Style(name='icon',type='Class')]
	
	/***
	 * default 0xFF0000
	 */
	[Style(name="overColor",type="String",inherit="yes")]
	
	/***
	 * default 0x0000FF
	 */
	[Style(name="outColor",type="String",inherit="yes")]
	
	/**
	 * 设置text文字样式，使用label的styleName直接将父容器样式赋值给label
	 */
	[Style(name="fontSize",type="Number",format="Length",inherit="yes")]
	[Style(name="fontFamily",type="String",inherit="yes")]
	[Style(name="fontWeight",type="String",enumeration="normal,bold",inherit="yes")]
	
	public class Action extends UIComponent
	{
		
		private var horizontalGap:Number = 2;
		private var verticalGap:Number = 2;
		
		private var paddingRight:int = 2
		
		private var _text:Label;
		private var _label:String;
		private var _icon:IFlexDisplayObject;
		
		public function Action()
		{
			super();
			mouseChildren = false;
			buttonMode = true;
			
			addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
		}
		
		/**
		 * 鼠标移入事件处理，文字加下划线同时变色
		 * @param	MouseEvent
		 */
		private function rollOverHandler(event:MouseEvent):void
		{
			var color:String = getStyle('overColor') || '0xFF0000';
			
			_text.setStyle('textDecoration', 'underline');
			_text.setStyle('color', color);
		}
		
		private function rollOutHandler(event:MouseEvent):void
		{
			var color:String = getStyle('outColor') || '0x0b333c';
			
			_text.setStyle('textDecoration', 'none');
			_text.setStyle('color', color);
		}
		
		/**
		 * 如果有图片样式，初始化图片，并将图片附件到父容器上
		 */
		private function iconView():void
		{
			var iconClass:Class = Class(getStyle('icon'))
			var icon:DisplayObject = getChildByName('icon');
			if (iconClass && !icon)
			{
				_icon = mx.core.IFlexDisplayObject(new iconClass());
				_icon.name = 'icon';
				this.addChild(DisplayObject(_icon));
			}
		}
		
		/**
		 * 图片样式发生变化时先移除上次的图片
		 */
		private function changeIcon():void
		{
			if (_icon)
				removeChild(DisplayObject(_icon));
		}
		
		/**
		 * 组件布局，首先计算字符串长度，然后计算图片长度和高度
		 * 默认父容器是长度和高度是比字符串和图片大的，如果小了没做处理
		 * @param	unscaledWidth
		 * @param	unscaledHeight
		 */
		private function layoutContents(unscaledWidth:Number, unscaledHeight:Number):void
		{
			var textWidth:int = 0;
			var textHeight:int = 0;
			var iconWidth:int = 0;
			var iconHeight:int = 0;
			
			var labelX:Number = 0;
			var labelY:Number = 0;
			var iconX:Number = 0;
			var iconY:Number = 0;
			
			var lineMetrics:TextLineMetrics;
			
			if (_text)//使用TextLineMetrics得到字符串长度和高度
			{
				lineMetrics = measureText(this._label);
				textWidth = lineMetrics.width + 4;
				textHeight = lineMetrics.height;
			}
			
			if (_icon)
			{
				iconWidth = _icon.width;
				iconHeight = _icon.height;
				iconX = paddingRight;
				iconY = (unscaledHeight - iconHeight) / 2;
				
				_icon.x = iconX;
				_icon.y = iconY;
			}
			
			if (textWidth > 0)
			{
				_text.width = Math.max(Math.min(unscaledWidth - iconWidth - horizontalGap, textWidth + 2), 0);
			}
			else
			{
				_text.width = 0;
			}
			_text.height = Math.min(unscaledHeight, textHeight + 3);
			
			labelX = iconWidth + paddingRight + (iconX > 0 ? horizontalGap : 0);
			labelY = (unscaledHeight - textHeight) / 2;
			
			this._text.x = labelX;
			this._text.y = labelY;
			
			if (_icon)
				setChildIndex(DisplayObject(_icon), numChildren - 1);
			if (_text)
				setChildIndex(DisplayObject(_text), numChildren - 1);
		
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			if (!_text)
			{
				_text = new Label();
				_text.styleName = this;
				addChild(_text);
			}
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			if (!_text)
			{
				_text = new Label();
				_text.styleName = this;
				addChild(_text);
			}
			
			if (!initialized)
			{
				iconView();
			}
		}
		
		override protected function measure():void
		{
			super.measure();
		}
		
		/**
		 * 组件每次发生变化时都会调用这个方法
		 * @param	unscaledWidth
		 * @param	unscaledHeight
		 */
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			iconView();
			layoutContents(unscaledWidth, unscaledHeight);
			if (_text.text != this._label)
			{
				_text.text = _label;
				
			}
		}
		
		override public function styleChanged(styleProp:String):void
		{
			super.styleChanged(styleProp);
			
			if ('icon' == styleProp)
			{
				changeIcon();
				iconView();
			}
		}
		
		public function set label(value:String):void
		{
			if (_label != value)
			{
				_label = value;
				
				invalidateSize();
				invalidateDisplayList();
			}
		}
		
		public function get label():String
		{
			return this._label;
		}
	}

}

